package ext.tianma.bom.workflow;

import java.util.ArrayList;

import wt.fc.WTObject;
import wt.fc.collections.WTArrayList;
import wt.lifecycle.LifeCycleManaged;
import wt.part.WTPart;
import wt.util.WTException;
import wt.vc.wip.WorkInProgressHelper;
import wt.workflow.engine.WfProcess;
import wt.workflow.engine.WfState;
import ext.tianma.version.ComToolUtil;

/**Description:【BOM审核确认流程】的业务处理逻辑  2013-09-18确认
 * @author    :  YangWen
 * @create at :  2013-9-22   上午10:01:23
 */
public class BOMReviewConfirmBusiness implements BOMWorkFlowInterface {


	/**
	 * 展示是否输出log信息
	 */
	private static boolean isShow = true;
    	
	
	/**
	 * 私有构造函数，防止实例化
	 */
	private BOMReviewConfirmBusiness(){
		
	}
	
	/**输出log信息
	 * @param info
	 */
	private static void out(String info){
		if(isShow)
			System.out.println("【BOM审核确认流程】的业务处理逻辑:"+BOMReviewConfirmBusiness.class.getName()+":"+info);
	}
	
		
	
	/**YW
	 * 展示页面数据
	 * @param pbo 业务主对象
	 * @param reviewObjs 随签列表
	 * @param tableData 不符合要求列表
	 * @throws WTException 
	 */
	public static void showPageData(WTObject pbo , WTArrayList reviewObjs , ArrayList<WTPart>  tableData) throws WTException{		
    	
    	if(ComToolUtil.isEmpty(pbo) || !(pbo instanceof WTPart))
    		return ;    	
    	WTPart part = (WTPart)pbo;    	
    	//递归获得所有的子部件
    	ArrayList<WTPart> allSubPart = BOMBusinessUtil.getAllSubPart(part);
    	out("指定对象【"+part.getDisplayIdentifier()+"】下包含的所有子部件个数是："+allSubPart.size());
    	
    	//初始化传入来的两个参数
    	if(reviewObjs == null)
    		reviewObjs = new WTArrayList();
    	else
    		reviewObjs.clear();
    	reviewObjs.add(part);
		if(tableData == null)
			tableData = new ArrayList<WTPart>();	
		else
			tableData.clear();
	   	    	    
	    //遍历pbo的BOM结构，只处理【原材料】和【半成品】，其它类型均不管
	    for (WTPart onePart : allSubPart) {	 	    
	    	//如果是【原材料】 或者 【半成品】对象 时	 
	    	out("【原材料】  RAWMATERIAL="+RAWMATERIAL);
	    	out("【半成品】  SEMIFINISHEDGOOD="+SEMIFINISHEDGOOD);	    	
	    	if(BOMBusinessUtil.isSubObject(onePart, RAWMATERIAL) || BOMBusinessUtil.isSubObject(onePart, SEMIFINISHEDGOOD)){
	    		
	    		
				LifeCycleManaged lcm = (LifeCycleManaged)onePart;	
				String currentState = lcm.getState().toString();
				out("当前对象【"+onePart.getDisplayIdentifier()+"】的生命周期是："+currentState);
				
				//如果是【原材料】，生命周期状态是【新品、批准、量产发布】，则不处理
				//NewProduct.value=新品
				//APPROVAL.value=批准
				//MassProductionRelease.value=量产发布
				if(BOMBusinessUtil.isSubObject(onePart, RAWMATERIAL)){
					if(currentState.equals("NewProduct")||currentState.equals("APPROVAL")||currentState.equals("MassProductionRelease"))
						continue;
				}
				
				//如果是【半成品】，生命周期状态是【新品、量产发布】，则不处理
				if(BOMBusinessUtil.isSubObject(onePart, SEMIFINISHEDGOOD)){
					if(currentState.equals("NewProduct")||currentState.equals("MassProductionRelease"))
						continue;
				}
				
				//如果生命周期状态为【废弃】，则视为不符合
				//OBSOLESCENCE.value=废弃
				if(currentState.equals("OBSOLESCENCE")){
					tableData.add(onePart);
					continue;
				}
				
				
	    		
	    		//如果关联了【正在运行】的流程，则视为不符合
				WfProcess runningProcess = null;
				ArrayList<WfProcess> referenceProcessList = BOMBusinessUtil.getRreferenceProcess(onePart);
				//如果不存在任务进程
				if(referenceProcessList != null && referenceProcessList.size() > 0){					
					for (WfProcess process : referenceProcessList) {
						//如果进程正在运行
					    if (process.getState().equals(WfState.OPEN_RUNNING)) {
					    	runningProcess = process;					    	
					    	break;
					    }
					}
				}
				
				if(runningProcess != null){
					tableData.add(onePart);
					continue;
				}
				//如果没关联【正在运行的流程】
				else{
					//如果生命周期状态为【新建物料】，则不符合要求
					if(currentState.equals("NewPart")){
						tableData.add(onePart);
						continue;
					}
					//如果生命周期状态为【正在工作】，则符合要求
					if(currentState.equals("INWORK")){
						reviewObjs.add(onePart);
						continue;
					}
			   }	    		
	    	}	    	
	    }
	    
	    //更新tableData的数据至最新版本
	    BOMBusinessUtil.getLatestWTPart(tableData);
	    
	    
		//保存被检出部件的编号
		ArrayList<String> checkoutPartNumberList = new ArrayList<String>();
		if(reviewObjs!=null && reviewObjs.size() > 0){
			for(int i=0;i<reviewObjs.size();++i){
				Object obj = null;
				try {
					obj = reviewObjs.getPersistable(i);
				} catch (WTException e) {
					e.printStackTrace();
					throw new WTException("从随签对象里获取第"+(i+1)+"个值时出错，程序终止！");						
				}
		    	if(obj == null || !(obj instanceof WTPart))
		    		continue;
		    		    	
		    	if(WorkInProgressHelper.isCheckedOut((WTPart)obj)){
				    checkoutPartNumberList.add(((WTPart)obj).getNumber());
				}
			}
		}
	    
	    /*if (!checkoutPartNumberList.isEmpty()) {
	    	String checkoutPart = "";
	    	for(String number : checkoutPartNumberList)
	    		checkoutPart += "\n" + number;
	    	throw new WTException("以下对象编号被检出："+checkoutPart);
	    }*/	 
	    
	}
	
	
}
